package cn.hesion;
/**
 * ClassName: NumOrderReducer <br/>
 * Description: <br/>
 * date: 2021/2/1 20:16<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @program: homework1_1
 * @description: 数据排序Reduce端
 * @author: hesion
 * @create: 2021-02-01 20:16
 **/
public class NumOrderReducer extends Reducer<IntWritable, IntWritable,IntWritable,IntWritable> {
    // context.write参数为IntWritable类型的，int无法直接传入，需要作层包装
    IntWritable index=new IntWritable(1);
    // 记录数字的索引值
    static  int i=1;
    @Override
    protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
       // 因为存在重复的数字，会将其变成一个key 这会导致输出的结果的数字会缺少
        // values记录了所有的数字的个数，循环遍历即可解决此问题
        for (IntWritable value : values) {
            index.set(i);
            context.write(index,key);
            // 索引数加1
            i++;
        }
    }
}
